package com.wyh.lotterysystem.service.impl;

import com.wyh.lotterysystem.controller.request.CreatePrizeRequest;
import com.wyh.lotterysystem.controller.request.PrizeListRequest;
import com.wyh.lotterysystem.dao.dataobject.PrizeDo;
import com.wyh.lotterysystem.dao.mapper.PrizeMapper;
import com.wyh.lotterysystem.service.PictureService;
import com.wyh.lotterysystem.service.PrizeService;
import com.wyh.lotterysystem.service.dto.PageDTO;
import com.wyh.lotterysystem.service.dto.PrizeListDTO;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@Service
public class PrizeServiceImpl implements PrizeService {

    @Autowired
    PrizeMapper prizeMapper;

    @Resource(name = "pictureServiceImpl")
    PictureService pictureService;

    @Override
    public Integer createPrize(CreatePrizeRequest param, MultipartFile picFile) {
        // 上传图片
        String fileName = pictureService.savePicture(picFile);
        // 存库
        PrizeDo prizeDO = new PrizeDo();
        prizeDO.setName(param.getPrizeName());
        prizeDO.setDescription(param.getDescription());
        prizeDO.setImageUrl(fileName);
        prizeDO.setPrice(param.getPrice());
        prizeMapper.insert(prizeDO);
        return prizeDO.getId();
    }

    @Override
    public PageDTO<PrizeListDTO> findPrizeList(PrizeListRequest request) {
        //总量
        Integer total = prizeMapper.count();

        //查找指定页中的奖品
        List<PrizeDo> prizeDos = prizeMapper.checkPagePrize(request.offset(),request.getPageSize());

        //装换
        List<PrizeListDTO> list = prizeDos.stream().map(prizeDo -> {
            PrizeListDTO prizeListDTO = new PrizeListDTO();
            prizeListDTO.setPrizeName(prizeDo.getName());
            prizeListDTO.setPrizeId(prizeDo.getId());
            prizeListDTO.setDescription(prizeDo.getDescription());
            prizeListDTO.setPrice(prizeDo.getPrice());
            prizeListDTO.setImageUrl(prizeDo.getImageUrl());
            return prizeListDTO;
        }).toList();

        return new PageDTO<>(total,list);
    }
}
